package com.suxin.module;

import lombok.Data;

/**
 * @Classname MyOpenTsdbConfig
 * @Description [ TODO ]
 * @Author Tang
 * @Date 2020/10/15 14:37
 * @Created by ASUS
 */
@Data
public class MyOpenTSDBConfig {

    private String host;

    private int port;

    private int httpConnectionPool;

    private int httpConnectTimeout;

    private int putConsumerThreadCount;

    private int batchPutSize;

    private int batchPutBufferSize;

    private int batchPutTimeLimit;

    private boolean readonly;

    public static class Builder{

        private String host;

        private int port;

        /**
         * 每个host分配的连接数
         */
        private int httpConnectionPool = 100;

        /**
         * 单位秒
         */
        private int httpConnectTimeout = 100;

        /**
         * 消费者线程数
         */
        private int putConsumerThreadCount = 2;

        /**
         * 每个http请求提交的数据大小
         */
        private int batchPutSize = 50;

        /**
         * 生产者消费者模式中，缓冲池的大小
         */
        private int batchPutBufferSize = 20000;

        /**
         * 每次提交等待的最大时间界限，ms
         */
        private int batchPutTimeLimit = 300;

        /**
         * 是否是只读
         */
        private boolean readonly = false;

        public Builder(String host, int port) {
            this.host = host;
            this.port = port;
        }

        public MyOpenTSDBConfig config() {
            MyOpenTSDBConfig config = new MyOpenTSDBConfig();
            config.host = this.host;
            config.port = this.port;
            config.httpConnectTimeout = this.httpConnectTimeout;
            config.httpConnectionPool = this.httpConnectionPool;
            config.putConsumerThreadCount = this.putConsumerThreadCount;
            config.batchPutSize = this.batchPutSize;
            config.batchPutBufferSize = this.batchPutBufferSize;
            config.batchPutTimeLimit = this.batchPutTimeLimit;
            config.readonly = this.readonly;
            return config;
        }

        public Builder httpConnectTimeout(int httpConnectTimeout) {
            // 判断
            this.httpConnectTimeout = httpConnectTimeout;
            return this;
        }

        public Builder httpConnectionPool(int httpConnectionPool) {
            this.httpConnectionPool = httpConnectionPool;
            return this;
        }

        public Builder putConsumerThreadCount(int putConsumerThreadCount) {
            if (putConsumerThreadCount < 1) {
                throw new IllegalArgumentException("The threadCount can't be less then 1");
            }
            this.putConsumerThreadCount = putConsumerThreadCount;
            return this;
        }

        public Builder batchPutSize(int batchPutSize) {
            if (batchPutSize < 1) {
                throw new IllegalArgumentException("The size can't be less then 1");
            }
            this.batchPutSize = batchPutSize;
            return this;
        }

        public Builder batchPutBufferSize(int batchPutBufferSize) {
            if (batchPutBufferSize < 1) {
                throw new IllegalArgumentException("The size can't be less then 1");
            }
            this.batchPutBufferSize = batchPutBufferSize;
            return this;
        }

        public Builder batchPutTimeLimit(int batchPutTimeLimit) {
            if (batchPutTimeLimit < 1) {
                throw new IllegalArgumentException("The time limit can't be less then 1");
            }
            this.batchPutTimeLimit = batchPutTimeLimit;
            return this;
        }

        public Builder readonly() {
            this.readonly = true;
            return this;
        }

    }

    /**
     * 创建Build对象
     * @param host
     * @param port
     * @return
     */
    public static Builder builder(String host, int port) {
        return new Builder(host, port);
    }

    public MyOpenTSDBClient connect() {
        return new MyOpenTSDBClient(this);
    }

}